package com.itany.corejava.code11_线程;

/**
 * @author MissChen
 * @date 2024年03月12日 15:00
 */
public class Test08_同步代码块 {
    public static void main(String[] args) {

        OneThread t1=new OneThread();
        OneThread t2=new OneThread();
        t1.setPriority(6);
        t1.setName("t1");
        t2.setName("t2");
        t1.start();
        t2.start();
    }
}
class OneThread extends Thread{
    static int num=100;
    static Object object=new Object();


    @Override
    public void run() {
       while(true){

           //同步代码块：被synchronized 包围的代码块
           //锁对象，要保证是相同的
           synchronized (object){
               if(num==0){
                   break;
               }else{
                   try {
                       Thread.sleep(10);
                       System.out.println(Thread.currentThread().getName()+"正在出票"+num--);
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }

               }
           }
       }
    }
}